﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.MemberInfo
{
    public partial class MemberInfoManage_E : PageBaseAdmin
    {
        #region 只读资料定义
        private readonly D_T_Member_Exp _Dal = new D_T_Member_Exp();

        #endregion

        #region 固定方法
        /// <summary>
        ///  设置按钮资料
        /// </summary>
        /// <param name="i"></param>
        /// <param name="but"></param>
        private void SetBut(int i, Button but)
        {
            AdminPublic.Try(() =>
            {
                switch(i)
                {
                    case 1:
                        but.CausesValidation = false;
                        break;
                    default:
                        break;
                }
            });

        }
        /// <summary>
        /// 初始化窗口控件
        /// </summary>
        private void ControlsInit()
        {
            AdminPublic.Try(() =>
            {
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add(new ListItem("男", "0"));
                DropDownList1.Items.Add(new ListItem("女", "1"));
            });
        }
        /// <summary>
        /// 编辑模式的时候,填充窗口
        /// </summary>
        /// <param name="model"></param>
        private void SetControlsData(M_T_Member model)
        {
            AdminPublic.Try(() =>
            {

                TextBox6.Text = model.AnswerA;
                TextBox8.Text = model.AnswerB;
                this.Calendar1.SelectedDate = model.Birthday;
                this.TextBox4.Text = model.Email;
                TextBox2.Text = model.IDCard;
                TextBox1.Text = model.Name;
                TextBox5.Text = model.ProblemA;
                TextBox7.Text = model.ProblemB;
                DropDownList1.SelectedIndex = model.Sex ? 1 : 0;
                TextBox3.Text = model.Tel;
            });
        }
        /// <summary>
        /// 从页面控件中构造一个实体model
        /// </summary>
        /// <returns></returns>
        private M_T_Member ControlsToModel()
        {
            var model = new M_T_Member()
            {
                Flag = true,
                id = ViewState["ID"].Exp_IntTryParse(),
                AnswerA = TextBox6.Text.Exp_Trim(),
                AnswerB = TextBox8.Text.Exp_Trim(),
                Birthday = this.Calendar1.SelectedDate,
                Email = this.TextBox4.Text.Exp_Trim(),
                FreezeDate = DateTime.Now,
                FreezeType = (int)M_T_Member_Exp.Freeze_Type.normal,
                Gold = 0,
                IDCard = TextBox2.Text.Exp_Trim(),
                Grade = 0,
                isApprove = true,
                Name = TextBox1.Text.Exp_Trim(),
                OpFreezeDate = DateTime.Now,
                OpFreezeType = (int)M_T_Member_Exp.Freeze_Type.normal,
                PassWord = "abc".Exp_Encrypt(),
                ProblemA = TextBox5.Text.Exp_Trim(),
                ProblemB = TextBox7.Text.Exp_Trim(),
                Sex = DropDownList1.SelectedIndex == 0 ? false : true,
                Tel = TextBox3.Text.Exp_Trim(),
                WinStock = 0
            };
            return model;
        }
        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if(iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, "会员管理", iRef);
                this.Exp_PageSkyAlterReplace("MemberInfoManage.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch(iRef)
                {
                    //case 0:
                    //    this.Exp_PageAlter("操作失败,操作中出现异常");
                    //    break;
                    //case -1:
                    //    this.Exp_PageAlter("操作失败,会员资料操作失败");
                    //    break;
                    //case -2:
                    //    this.Exp_PageAlter("操作失败,权限组资料操作失败");
                    //    break;
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }
        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {


            AdminPublic.Try(() =>
            {

                if(!IsPostBack)
                {
                    //初始化权限组下拉框
                    ControlsInit();
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    if(id > 0)
                    {
                        var model = _Dal.GetModel(id);
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("MemberInfoManage.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            SetControlsData(model);
                        }
                    }
                }
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);
            });

        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //注意,统一,先获取实体,然后根据实体进行各种判断
                var model = ControlsToModel();

                var LogType = Log_Type.add;

                //if(_Dal.IsLoginName(model.Name, model.id))
                //{
                //    this.Exp_PageAlter("用户名不可以重复");
                //    return;
                //}

                var iRef = 0;
                if(0 >= model.id)
                {
                    iRef = _Dal.Add(model);
                }
                else
                {
                    //    修改的专用判断
                    LogType = Log_Type.update;
                    var Temp = _Dal.ExistsFlag(model.id);
                    if(!Temp)
                    {
                        this.Exp_PageAlter("资料不存在,可能被其他人删除");
                        return;
                    }

                    iRef = _Dal.UpdateBase(model);
                }
                EditSaveMsg(iRef, LogType);
            });

        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("MemberInfoManage.aspx?" + sKey);
            });
        }
    }
}
